﻿@model WebApp.Models.BillDetail
@{
  ViewData["Title"] = "BillDetail";
  ViewData["PageName"] = "BillDetails_Index";
  ViewData["Heading"] = "<i class='fal fa-window text-primary'></i>  BillDetail";
  ViewData["Category1"] = "Home";
  ViewData["PageDescription"] = "";
}
@section HeadBlock {
  <link href="@Fingerprint.Tag("/Content/css/notifications/toastr/toastr.css")" rel="stylesheet" />
  <link href="@Fingerprint.Tag("/Scripts/easyui/themes/insdep/easyui.css")" rel="stylesheet" />
  <link href="@Fingerprint.Tag("/Content/css/formplugins/bootstrap-daterangepicker/bootstrap-daterangepicker.css")" rel="stylesheet" />
}
<div class="row">
  <div class="col-lg-12 col-xl-12">
    <div id="panel-1" class="panel">
      <div class="panel-hdr">
        <h2>
          BillDetail
        </h2>
        <div class="panel-toolbar">
          <button class="btn btn-panel bg-transparent fs-xl w-auto h-auto rounded-0" data-action="panel-collapse" data-toggle="tooltip" data-offset="0,10" data-original-title="Collapse"><i class="fal fa-window-minimize"></i></button>
          <button class="btn btn-panel bg-transparent fs-xl w-auto h-auto rounded-0" data-action="panel-fullscreen" data-toggle="tooltip" data-offset="0,10" data-original-title="Fullscreen"><i class="fal fa-expand"></i></button>
          @*<button class="btn btn-panel bg-transparent fs-xl w-auto h-auto rounded-0" data-action="panel-close" data-toggle="tooltip" data-offset="0,10" data-original-title="Close"><i class="fal fa-trash-alt"></i></button>*@
        </div>

      </div>
      <div class="panel-container enable-loader show">
        <div class="loader"><i class="fal fa-spinner-third fa-spin-4x fs-xxl"></i></div>
        <div class="panel-content py-2 rounded-bottom border-faded border-left-0 border-right-0  text-muted bg-faded bg-subtlelight-fade">
          <div class="row no-gutters align-items-center">
            <div class="col">
              <!-- 开启授权控制请参考 @@if (Html.IsAuthorize("Create") -->
               <div class="btn-group btn-group-sm">
                <button name="searchbutton" class="btn btn-default"> <span class="fal fa-search mr-1"></span> @Html.L("Refresh") </button>
              </div>
              <div class="btn-group btn-group-sm">
                <button name="appendbutton" class="btn btn-default"> <span class="fal fa-plus mr-1"></span> @Html.L("Add") </button>
              </div>
              <div class="btn-group btn-group-sm">
                <button name="deleterowbutton" disabled class="btn btn-default"> <span class="fal fa-trash-alt mr-1"></span> @Html.L("Delete") </button>
              </div>
              
              <div class="btn-group btn-group-sm hidden-xs">
                <button name="importexcelbutton" type="button"  class="btn btn-default"><span class="fal fa-cloud-upload mr-1"></span> @Html.L("Import") </button>
                <button type="button" class="btn btn-default dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                  <span class="sr-only">Toggle Dropdown</span>
                </button>
                <div class="dropdown-menu">
                  <button name="downloadtemplatebutton" class="dropdown-item js-waves-on"><span class="fal fa-download"></span> @Html.L("Download") </button>
                </div>
              </div>
              <div class="btn-group btn-group-sm ">
                <button name="exportexcelbutton" class="btn btn-default"> <span class="fal fa-file-excel mr-1"></span>  @Html.L("Export") </button>
              </div>
              <div class="btn-group btn-group-sm ">
                <button name="helpbutton" class="btn btn-default"> <span class="fal fa-question-circle mr-1"></span> @Html.L("Help") </button>
              </div>
            </div>
          </div>
        </div>
        <div class="panel-content">
          <div class="table-responsive">
            <table id="billdetails_datagrid">
            </table>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
 
@section ScriptsBlock {
 @{ Html.RenderPartial("_ImportWindow",new {template="BillDetail" ,model="BillDetail",url="/BillDetails/ImportData",callback = "reloadData()",autosave=false }); }		 
 @{ Html.RenderPartial("_PopupDetailFormView",new WebApp.Models.BillDetail()); }
  <script src="@Fingerprint.Tag("/Scripts/notifications/toastr/toastr.js")"></script>
  <script src="@Fingerprint.Tag("/Scripts/formplugins/bootstrap-daterangepicker/bootstrap-daterangepicker.js")"></script>
  <script src="@Fingerprint.Tag("/Scripts/easyui/jquery.easyui.min.js")"></script>
  <script src="@Fingerprint.Tag("/Scripts/easyui/plugins/datagrid-filter.js")"></script>
  <script src="@Fingerprint.Tag("/Scripts/easyui/locale/easyui-lang-zh_CN.js")"></script>
  <script src="@Fingerprint.Tag("/Scripts/jquery.easyui.component.js")"></script>
  <script src="@Fingerprint.Tag("/Scripts/jquery.extend.formatter.js")"></script>
  <script src="@Fingerprint.Tag("/Scripts/jquery.custom.extend.js")"></script>
  <script src="@Fingerprint.Tag("/Scripts/plugin/jquery.serializejson/jquery.serializejson.min.js")"></script>
  <script src="@Fingerprint.Tag("/Scripts/plugin/filesaver/FileSaver.min.js")"></script>
  <script src="@Fingerprint.Tag("/Scripts/plugin/filesaver/jquery.fileDownload.js")"></script>
<script type="text/javascript">
    //绑定按钮事件
    $('button[name="searchbutton"]').on('click', reloadData);
    $('button[name="appendbutton"]').on('click', appendData);
    $('button[name="deleterowbutton"]').on('click', deleteRow);
    $('button[name="exportexcelbutton"]').on('click', exportExcel);
    $('button[name="importexcelbutton"]').on('click', () => {
      importExcel('BillDetail')
    });
    $('button[name="downloadtemplatebutton"]').on('click', () => {
      downloadTemplate('BillDetail')
    });
    //全屏事件
    document.addEventListener('panel.onfullscreen', () => {
         $dg.datagrid('resize');
     });
      //是否强制从后台取值
      const REQUIRBACKEND = false;
      //是否开启行内编辑
      const EDITINLINE = false;
      var billdetail = {};

       //执行导出下载Excel
       function exportExcel() {
                const filterRules = JSON.stringify($dg.datagrid('options').filterRules);
                //console.log(filterRules);
                const dialog = bootbox.dialog({
                    message: '<div class="text-center"><i class="fal fa-spinner fa-pulse fa-lg mr-1"></i> 正在执行...</div>',
                    closeButton: false
                 });
                let formData = new FormData();
                formData.append('filterRules', filterRules);
                formData.append('sort', 'Id');
                formData.append('order', 'asc');
                $.postDownload('/BillDetails/ExportExcel', formData).then(res => {
                    setTimeout(() => {
                       dialog.modal('hide')
                     }, 300);
                    toastr.success('导出成功!');
                }).catch(err => {
                    setTimeout(() => {
                       dialog.modal('hide')
                     }, 300);
                    $.messager.alert('导出失败', err.statusText, 'error');
                });

        }
        //显示帮助信息
        function dohelp() {
            toastr.info('有问题请联系管理员 <br><i class="fa fa-envelope-o" aria-hidden="true"></i> : demo@admin.com','帮助');  
        }
       var editIndex = undefined;
       //重新加载数据
       function reloadData() {
            $dg.datagrid('unselectAll');
            $dg.datagrid('uncheckAll');
            $dg.datagrid('reload');
       }
    //新增记录
    async function appendData() {
       billdetail = {  };
        //需要后台创建对象
       if (REQUIRBACKEND) {
         billdetail = await $.get('/BillDetails/NewItem');
       }
           //弹出新增窗口
           openbilldetaildetailwindow(billdetail, 'Added');
    }
    //删除编辑的行
    function deleteRow() {
    deleteChecked();
    }
    //删除选中的行
    function deleteChecked() {
        const checked = $dg.datagrid('getChecked').filter(item=>item.Id!=null && item.Id > 0).map(item => {
                   return item.Id;
                });;
        if (checked.length > 0) {
              deleteRows(checked);
         } else {
                $.messager.alert('提示', '请先选择要删除的记录!','question');
         }
     }
    //执行删除
    function deleteRows(selected){
      $.messager.confirm('确认', `你确定要删除这 <span class='badge badge-icon position-relative'>${selected.length} </span> 行记录?`, result => {
       if (result) {
         $.post('/BillDetails/DeleteChecked', { id: selected })
                         .done(response => {
                                if (response.success) {
                                    toastr.error(`成功删除 [${selected.length}] 行记录`);  
                                    reloadData();
                                } else {
                                    $.messager.alert('错误', response.err,'error');
                                }
                          })
                          .fail((jqXHR, textStatus, errorThrown) => {
                             $.messager.alert('异常', `${jqXHR.status}: ${jqXHR.statusText} `, 'error');
                          });
        }
      });
    }
    //弹出明细信息
    async function showdetailsWindow(id,index) {
        const billdetail = $dg.datagrid('getRows')[index];
        if (REQUIRBACKEND) {
           billdetail = await $.get('/BillDetails/GetItem/' + id);
        }
        openbilldetaildetailwindow(billdetail,'Modified');
    }

    //初始化定义datagrid
    var $dg = $('#billdetails_datagrid');
    $(() => {
        //定义datagrid结构
       $dg.datagrid({
                        //fit: false,
        height:670,
         pageSize:15,
                        rownumbers:true,
                        checkOnSelect:false,
                        selectOnCheck:false,
                        idField:'Id',
                        sortName:'Id',
                        sortOrder:'desc',
                        remoteFilter: true,
                        singleSelect: true,
                        //url: '/BillDetails/GetData',
                        method: 'post',
                        pagination: true,
                        clientPaging:false,
                        striped:true,
                        pageList: [10, 20, 50, 100, 500,2000],
                        filterRules: [],
                        onBeforeLoad: function () {
                        $('.enable-loader').removeClass('enable-loader')
                        },
                        onLoadSuccess: function (data) {
                          $("button[name*='deleterowbutton']").prop('disabled', true);
                        },
                        onCheck: function () {
                          $("button[name*='deleterowbutton']").prop('disabled', false);
                        },
                        onUncheck: function () {
                           const checked = $(this).datagrid('getChecked').length > 0;
                           $("button[name*='deleterowbutton']").prop('disabled', !checked);
                        },
                        onCheckAll: function (rows) {
                          if(rows.length > 0){
                            $("button[name*='deleterowbutton']").prop('disabled', false);
                          }
                        },
                        onUncheckAll: function () {
                         $("button[name*='deleterowbutton']").prop('disabled', true);
                        },
                        onSelect: function(index,row) {
                           billdetail = row;
                        },
                        frozenColumns: [[
                         /*开启CheckBox选择功能*/
                         { field: 'ck', checkbox: true },
                         { 
                            field: 'action',
                            title:'@Html.L("Command")',
                            align:'center',
                            width: 85,
                            sortable: false,
                            resizable: true,
                            formatter: function showdetailsformatter(value, row, index) {
                                         if (!row.editing) {
                                            return `<div class="btn-group">\
                                                         <button onclick="showdetailsWindow('${row.Id}',  ${index})" class="btn btn-outline-info btn-sm btn-icon waves-effect waves-themed" data-toggle="tooltip" data-placement="right" title="@Html.L("EditDetail")" ><i class="fal fa-edit"></i> </button>\
                                                         <button onclick="deleteRows(['${row.Id}'],${index})" class="btn btn-outline-info btn-sm btn-icon waves-effect waves-themed" data-toggle="tooltip" data-placement="right" title="@Html.L("Delete")" ><i class="fal fa-trash-alt"></i> </button>\
                                                    </div>`;
                                         } else {
                                             return `<button class="btn btn-outline-info btn-sm btn-icon waves-effect waves-themed" disabled title="查看明细"  ><i class="fal fa-edit"></i> </button>`;
                                         }
                                     }
                          }
                        ]],
                        columns: [[
            
            {   /*BillId*/
                field:'BillId',
                title:'@Html.DisplayNameFor(model => model.BillId)',
                width:160, 
                sortable:true, 
                resizable:true,
                hidden:false,
                formatter:(value,row) => {
                        return row.BillHeaderBillNo;
                    },
            },
            {    /*表名*/
                 field:'MeterName', 
                 title:'@Html.DisplayNameFor(model => model.MeterName)', 
                 width:200,
                 hidden:false,
                 sortable:true,
                 resizable:true 
            },
            {    /*表序号*/
                 field:'LineNo', 
                 title:'@Html.DisplayNameFor(model => model.LineNo)', 
                 width:200,
                 hidden:false,
                 sortable:true,
                 resizable:true 
            },
            {    /*表号*/
                 field:'MeterId', 
                 title:'@Html.DisplayNameFor(model => model.MeterId)', 
                 width:120,
                 hidden:false,
                 sortable:true,
                 resizable:true 
            },
            {    /*出线名称*/
                 field:'MeterName1', 
                 title:'@Html.DisplayNameFor(model => model.MeterName1)', 
                 width:200,
                 hidden:false,
                 sortable:true,
                 resizable:true 
            },
            {    /*安装位置*/
                 field:'MeterPoint', 
                 title:'@Html.DisplayNameFor(model => model.MeterPoint)', 
                 width:260,
                 hidden:false,
                 sortable:true,
                 resizable:true 
            },
            {    /*正负项*/
                 field:'Negitive', 
                 title:'@Html.DisplayNameFor(model => model.Negitive)', 
                 width:120,
                 hidden:false,
                 sortable:true,
                 resizable:true 
            },
            {    /*占比*/
                 field:'Ratio', 
                 title:'@Html.DisplayNameFor(model => model.Ratio)', 
                 width:100, 
                 align:'right',
                 hidden:false,
                formatter:numberformatter,
                sortable:true,
                resizable:true 
            },
            {    /*用水量(m3)*/
                 field:'Water', 
                 title:'@Html.DisplayNameFor(model => model.Water)', 
                 width:100, 
                 align:'right',
                 hidden:false,
                formatter:numberformatter,
                sortable:true,
                resizable:true 
            },
            {    /*上个月用水量(m3)*/
                 field:'LastWater', 
                 title:'@Html.DisplayNameFor(model => model.LastWater)', 
                 width:100, 
                 align:'right',
                 hidden:false,
                formatter:numberformatter,
                sortable:true,
                resizable:true 
            },
            {    /*环比(%)*/
                 field:'PerCent', 
                 title:'@Html.DisplayNameFor(model => model.PerCent)', 
                 width:100, 
                 align:'right',
                 hidden:false,
                formatter:numberformatter,
                sortable:true,
                resizable:true 
            },
            {    /*实际用水量(m3)*/
                 field:'ActualWater', 
                 title:'@Html.DisplayNameFor(model => model.ActualWater)', 
                 width:100, 
                 align:'right',
                 hidden:false,
                formatter:numberformatter,
                sortable:true,
                resizable:true 
            },
            {   /*期初日期*/
                field:'WaterDt1', 
                title:'@Html.DisplayNameFor(model => model.WaterDt1)', 
                width:100, 
                align:'right',
                hidden:false,
                formatter:dateformatter, 
                sortable:true, 
                resizable:true
            } ,
            {    /*期初水量(m3)*/
                 field:'Water1', 
                 title:'@Html.DisplayNameFor(model => model.Water1)', 
                 width:100, 
                 align:'right',
                 hidden:false,
                formatter:numberformatter,
                sortable:true,
                resizable:true 
            },
            {   /*期末日期*/
                field:'WaterDt2', 
                title:'@Html.DisplayNameFor(model => model.WaterDt2)', 
                width:100, 
                align:'right',
                hidden:false,
                formatter:dateformatter, 
                sortable:true, 
                resizable:true
            } ,
            {    /*期末水量(m3)*/
                 field:'Water2', 
                 title:'@Html.DisplayNameFor(model => model.Water2)', 
                 width:100, 
                 align:'right',
                 hidden:false,
                formatter:numberformatter,
                sortable:true,
                resizable:true 
            },
            {    /*备注*/
                 field:'Remark', 
                 title:'@Html.DisplayNameFor(model => model.Remark)', 
                 width:260,
                 hidden:false,
                 sortable:true,
                 resizable:true 
            },
                 ]]
           }).datagrid('enableFilter',[
                {   /*占比*/
                      field: 'Ratio',
                      type: 'numberbox',
                      op:['equal','notequal','less','lessorequal','greater','greaterorequal']
                  },
                {   /*用水量(m3)*/
                      field: 'Water',
                      type: 'numberbox',
                      op:['equal','notequal','less','lessorequal','greater','greaterorequal']
                  },
                {   /*上个月用水量(m3)*/
                      field: 'LastWater',
                      type: 'numberbox',
                      op:['equal','notequal','less','lessorequal','greater','greaterorequal']
                  },
                {   /*环比(%)*/
                      field: 'PerCent',
                      type: 'numberbox',
                      op:['equal','notequal','less','lessorequal','greater','greaterorequal']
                  },
                {   /*实际用水量(m3)*/
                      field: 'ActualWater',
                      type: 'numberbox',
                      op:['equal','notequal','less','lessorequal','greater','greaterorequal']
                  },
                {   /*期初水量(m3)*/
                      field: 'Water1',
                      type: 'numberbox',
                      op:['equal','notequal','less','lessorequal','greater','greaterorequal']
                  },
                {   /*期末水量(m3)*/
                      field: 'Water2',
                      type: 'numberbox',
                      op:['equal','notequal','less','lessorequal','greater','greaterorequal']
                  },
                {     /*期初日期*/
                      field: 'WaterDt1',
                      type: 'dateRange',
                      options: {
                      }
                  },
                {     /*期末日期*/
                      field: 'WaterDt2',
                      type: 'dateRange',
                      options: {
                      }
                  },
           {   /*BillId*/
                 field: 'BillId',
                 type:'combobox',
                 options:{  
                                panelHeight:'auto',
                                valueField:'Id',
                                textField:'BillNo',
                                method:'get',
                                url:'/BillDetails/GetBillHeaders',
                                onChange: value => {
                                if (value === '' || value === null) {
                                    $dg.datagrid('removeFilterRule', 'BillId');
                                } else {
                                    $dg.datagrid('addFilterRule', {
                                        field: 'BillId',
                                        op: 'equal',
                                        value: value
                                });
                            }
                            $dg.datagrid('doFilter');
                        }
                    }
           },
           ])
           .datagrid('load', '/BillDetails/GetData');
           
     });
 
</script>



}

 
